﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;

namespace UpdateLcdDateTime
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private SerialPort serialPort = new SerialPort();
        private bool OfforOn = false;
        private DispatcherTimer clockTimer = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();

            ComBoxPort.ItemsSource = SerialPort.GetPortNames();

            InitStartTimer();
        }

        public static UInt16 CRC16(List<byte> crcSrc, UInt16 length)
        {
            UInt16 crcReg = 0xffff;

            for (UInt16 i = 0; i < length; i++)
            {
                crcReg = (UInt16)((crcReg) ^ crcSrc[i]);
                for (UInt16 j = 0; j < 8; j++)
                {
                    if (0x0001 == (crcReg & 0x0001))
                    {
                        crcReg >>= 1;
                        crcReg ^= 0xa001;
                    }
                    else
                    {
                        crcReg >>= 1;
                    }
                }
            }
            return crcReg;
        }

        private void RealTime(object sender, EventArgs e)
        {
            string year;
            string month;
            string day;
            string hour;
            string minute;
            string second;

            year = System.DateTime.Now.Year.ToString();
            month = System.DateTime.Now.Month.ToString();
            day = System.DateTime.Now.Day.ToString();
            hour = System.DateTime.Now.Hour.ToString();
            minute = System.DateTime.Now.Minute.ToString();
            second = System.DateTime.Now.Second.ToString();

            if (month.Length == 1)
            {
                month = "0" + month;
            }
            if (day.Length == 1)
            {
                day = "0" + day;
            }
            if (hour.Length == 1)
            {
                hour = "0" + hour;
            }
            if (minute.Length == 1)
            {
                minute = "0" + minute;
            }
            if (second.Length == 1)
            {
                second = "0" + second;
            }

            TextBoxSend.Text = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        }

        private void InitStartTimer()
        {
            clockTimer.IsEnabled = true;
            clockTimer.Interval = TimeSpan.FromSeconds(1);
            clockTimer.Tick += RealTime;
            clockTimer.Start();
        }

        private void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            if (OfforOn == false)
            {
                int baud = 0;

                int.TryParse(ComBoxBaudRate.Text, out baud);

                serialPort.PortName = ComBoxPort.Text;
                serialPort.BaudRate = baud;
                serialPort.Parity = Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;

                try
                {
                    serialPort.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                OfforOn = true;
                BtnOpen.Content = "Close";
                //BtnOpen.Background = Brushes.Green;
                tbFlag.Background = Brushes.Green;
            }
            else
            {
                try
                {
                    serialPort.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                OfforOn = false;
                BtnOpen.Content = "Open";
                //BtnOpen.Background = Brushes.Red;
                tbFlag.Background = Brushes.Red;
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ComBoxPort.ItemsSource = SerialPort.GetPortNames();
        }

        private void BtnGetTime_Click(object sender, RoutedEventArgs e)
        {
            string year = System.DateTime.Now.Year.ToString();
            string month = System.DateTime.Now.Month.ToString();
            string day = System.DateTime.Now.Day.ToString();
            string hour = System.DateTime.Now.Hour.ToString();
            string minute = System.DateTime.Now.Minute.ToString();
            string second = System.DateTime.Now.Second.ToString();

            if (month.Length == 1)
            {
                month = "0" + month;
            }
            if (day.Length == 1)
            {
                day = "0" + day;
            }
            if (hour.Length == 1)
            {
                hour = "0" + hour;
            }
            if (minute.Length == 1)
            {
                minute = "0" + minute;
            }
            if (second.Length == 1)
            {
                second = "0" + second;
            }

            TextBoxSend.Text = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        }

        static byte HEXtoBCD(byte c)
        {
            byte t;

            t = (byte)((c % 100) / 10);
            t <<= 4;
            t |= (byte)(c % 10);

            return t;
        }

        private void BtnSend_Click(object sender, RoutedEventArgs e)
        {
            List<byte> send = new List<byte>();

            try
            {
                byte device = byte.Parse(TextBoxDevice.Text);
                UInt16 address = UInt16.Parse(TextBoxAddress.Text);



                send.Add(device);
                send.Add(0x10);
                send.Add((byte)(address >> 8));
                send.Add((byte)(address & 0xff));
                send.Add(0x00);
                send.Add(0x03);
                send.Add(0x06);
                send.Add((byte)(System.DateTime.Now.Year - 2000));
                send.Add((byte)(System.DateTime.Now.Month));
                send.Add((byte)(System.DateTime.Now.Day));
                send.Add((byte)(System.DateTime.Now.Hour));
                send.Add((byte)(System.DateTime.Now.Minute));
                send.Add((byte)(System.DateTime.Now.Second));

                UInt16 crc = CRC16(send, (UInt16)send.Count);

                send.Add((byte)(crc & 0xff));
                send.Add((byte)(crc >> 8));

                serialPort.Write(send.ToArray(), 0, send.Count);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
